#include "mq_connection.hpp"

void cb(yqx::Channel::ptr& channel, std::string consumer_tag, const yqx::BasicProperties* bp, const std::string& body){
    std::cout << consumer_tag << "消费了消息：" << body << std::endl;
    channel->basicAck(bp->id());
}

int main(int argc, char* argv[]){
    if(argc != 2){
        printf("usage:./consume_clinet queue1\n");
        return -1;
    }
    //实例化异步工作线程对象
    yqx::AsyncWorker::ptr awp = std::make_shared<yqx::AsyncWorker>();
    //实例化连接对象
    yqx::Connection::ptr conn = std::make_shared<yqx::Connection>("127.0.0.1", 8085, awp);
    //通过连接创建信道
    yqx::Channel::ptr channel = conn->openChannel();
    //通过信道提供的服务完成所需
    channel->declareExchange("exchange1", yqx::ExchangeType::FANOUT, true, false);
    channel->declareQueue("queue1", true, false, false);
    channel->declareQueue("queue2", true, false, false);
    channel->bind("exchange1", "queue1", "queue1");
    channel->bind("exchange1", "queue2", "news.music.#");
    auto functor = std::bind(cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->basicConsume("consumer1", argv[1], false, functor);
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));
    conn->closeChannel(channel);
}